// pages/destination/destination.js

var app = getApp();
var url = app.globalData.url;
var headers = app.globalData.headers;
Page({
  data:{
    destination:{
      bdata: [],
      idx: 0,
      rightList: [],
      mainId: 1
    }
    // tabBar: {
    //   index: {
    //     img: "index_.png"
    //   },
    //   classfiy: {
    //     img: "sort_.png"
    //   },
    //   trval: {
    //     img: "expert_icon.png"
    //   },
    //   goal: {
    //     img: "adr.png"
    //   },
    //   my: {
    //     img: "my_.png"
    //   }
    // }
  },
  // toindex: function () {
  //   wx.redirectTo({
  //     url: '../index/index',
  //   })
  // },
  // tosort: function () {
  //   wx.redirectTo({
  //     url: '../sort/sort',
  //   })
  // },
  // togoal: function () {
  //   wx.redirectTo({
  //     url: '../destination/destination',
  //   })
  // },
  // tomy: function () {
  //   wx.redirectTo({
  //     url: '../my/my',
  //   })
  // },
  search:function(e){ 
    console.log(e)
    var mainId = e.currentTarget.dataset.mainid;
    var thirdId = e.currentTarget.dataset.thirdid;
    var name = e.currentTarget.dataset.city;
    wx.navigateTo({
      url: '../../pages/list/list?mainId=' + mainId + '&thirdId=' + thirdId+'&name='+name+'',
    })
    // Router.navto({
    //   url:"../../pages/list/list",
    //   data:{
    //     city:e.target.dataset.city,
    //     page:"目的地"
    //   }
    // })
  },
  select:function(e){ 
    var destination = this.data.destination;
    var dmainId = destination.bdata[e.target.dataset.i].id
    destination.mainId = dmainId;
    destination.idx = e.target.dataset.i; 
    this.setData({ 
      destination: destination
    })
    
    this.showRight(dmainId)
  },
  showRight: function (mainId) {
    var destination = this.data.destination;
    let that=this
    wx.request({
      url: url + 'api/get_destination_list',
      header: headers,
      data: {
        "main_type_id": mainId
      },
      success: function (data) {
        if(data.data.result===1){
          destination.rightList = data.data.data
          that.setData({
            destination:destination
          })
        }else{
          wx.showModal({
            title: '来自伴行的提示',
            content: data.data.message,
          })
        }
        
      }
    })
  },
  Load:function(){
    var that = this;
    let mainId;
    var destination = this.data.destination
    try {
      var value = JSON.parse(wx.getStorageSync('mainPlace'));
      destination.bdata = value
      if (value) {
        // Do something with return value

        that.setData({

          destination: destination
        })
      }
    } catch (e) {
      // Do something when catch error
    };

    mainId = destination.bdata[0].id;

    this.showRight(mainId)
  },
  onLoad:function(){
    this.Load()
   
  }
})